<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      size="small"
    >
      <el-form-item label="选手" prop="playerName">
        <el-select
          v-model="queryParams.playerName"
          placeholder="请选择选手"
          clearable
          size="small"
        >
          <el-option
            v-for="option in playerOptions"
            :key="option.value"
            :label="option.label"
            :value="option.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择类型"
          clearable
          size="small"
        >
          <el-option label="图文" value="image"></el-option>
          <el-option label="视频" value="video"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增动态</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格区域 -->
    <el-table v-loading="loading" :data="tableData">
      <el-table-column
        prop="id"
        label="ID"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="playerName"
        label="选手"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column prop="type" label="类型" width="100" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type === 'image'" type="success">图文</el-tag>
          <el-tag v-if="scope.row.type === 'video'" type="primary">视频</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="content"
        label="文案"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column label="媒体内容" width="120" align="center">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.type === 'image'"
            style="width: 50px; height: 50px"
            :src="scope.row.mediaUrl"
            :preview-src-list="[scope.row.mediaUrl]"
          >
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
          <el-button
            v-else-if="scope.row.type === 'video'"
            type="text"
            @click="handlePlayVideo(scope.row)"
          >
            播放视频
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="180"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 视频播放弹窗 -->
    <el-dialog
      title="视频播放"
      :visible.sync="videoDialogVisible"
      width="50%"
      @close="handleCloseVideo"
    >
      <div class="video-container">
        <video
          v-if="currentVideoUrl"
          :src="currentVideoUrl"
          controls
          style="width: 100%"
          ref="videoPlayer"
        ></video>
      </div>
    </el-dialog>

    <!-- 添加编辑组件 -->
    <edit-square
      :visible.sync="editDialogVisible"
      @success="handleEditSuccess"
    />
  </div>
</template>

<script>
import { listPlayer } from "@/api/player";
import { parseTime } from "@/utils/ruoyi";
import EditSquare from "./components/edit";

export default {
  name: "PlayerSquare",
  components: {
    EditSquare,
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      tableData: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        playerName: undefined,
        type: undefined,
      },
      // 选手选项
      playerOptions: [],
      // 视频播放相关
      videoDialogVisible: false,
      currentVideoUrl: "",
      // 添加编辑弹窗控制变量
      editDialogVisible: false,
    };
  },
  created() {
    this.getPlayerOptions();
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // TODO: 调用后端接口获取数据
      setTimeout(() => {
        this.loading = false;
        // 模拟数据
        this.tableData = [
          {
            id: 1,
            playerName: "选手1",
            type: "image",
            content: "这是一条图文动态",
            mediaUrl: "https://example.com/image.jpg",
            createTime: new Date(),
          },
        ];
        this.total = 1;
      }, 1000);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 获取选手下拉列表 */
    getPlayerOptions() {
      listPlayer().then((response) => {
        this.playerOptions = response.rows.map((item) => ({
          label: item.name,
          value: item.id,
        }));
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.editDialogVisible = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editDialogVisible = true;
      // TODO: 需要把当前行数据传递给编辑组件
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // TODO: 实现删除逻辑
    },
    /** 播放视频 */
    handlePlayVideo(row) {
      this.currentVideoUrl = row.mediaUrl;
      this.videoDialogVisible = true;
    },
    /** 关闭视频 */
    handleCloseVideo() {
      if (this.$refs.videoPlayer) {
        this.$refs.videoPlayer.pause();
      }
      this.videoDialogVisible = false;
      this.currentVideoUrl = "";
    },
    /** 时间格式化 */
    parseTime,
    /** 编辑成功回调 */
    handleEditSuccess() {
      this.$modal.msgSuccess("操作成功");
      this.getList();
    },
  },
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
.mb8 {
  margin-bottom: 8px;
}
.video-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
}
</style>
